package com.taptech.doufu.ui.view.search;

import android.app.Activity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ProgressBar;

import com.taptech.doufu.util.StringUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.bean.cartoon.CartoonBean;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.services.DiaobaoSearchService;
import com.taptech.doufu.ui.view.drawcircle.MyRecyclerView;
import com.taptech.doufu.ui.view.drawcircle.MySwipeRefreshLayout;
import com.taptech.doufu.ui.view.drawcircle.RecyclerViewAdapterAsListView;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.util.UIUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;

/**
 * 漫画搜索
 */
public class SearchCartoonView implements HttpResponseListener,SearchBaseView {

    // UI控件
    protected MySwipeRefreshLayout swipeLayout;
    protected MyRecyclerView listView;
    protected RecyclerViewAdapterAsListView adapter;
    protected ProgressBar waitBar;
    private View loadView ,nullContentLayout;


    private Activity activity;
    private View rootView;
    private String currentKey = "";


    // 数据
    public List<MineAbstractBean> mDataList = new ArrayList<>();
    protected String last = "";
    protected boolean hasMoreContent = true;



    public SearchCartoonView(Activity activity, View rootView) {
        // TODO Auto-generated constructor stub
        this.activity = activity;
        this.rootView = rootView;
        initView();
    }

    protected void initView() {
        loadView = rootView.findViewById(R.id.empty_load_root_layout);
        nullContentLayout = rootView.findViewById(R.id.empty_load_layout_null_content);
        waitBar = (ProgressBar) rootView.findViewById(R.id.xlistview_footer_progressbar);
        swipeLayout = (MySwipeRefreshLayout) rootView.findViewById(R.id.swipe_container);
        swipeLayout.setOnRefreshListener(new MySwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
//                listViewRefresh();
                //关闭刷新动画
                swipeLayout.setRefreshing(false);
                //关闭下拉使能已免
                swipeLayout.setEnabled(false);
                //显示顶部刷新等待窗
//                waitBar.setVisibility(View.VISIBLE);
            }
        });
        adapter = new RecyclerViewAdapterAsListView(activity, mDataList);
        listView = (MyRecyclerView) rootView.findViewById(R.id.recycler_view_id);
        RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        listView.setLayoutManager(layoutManager);
        listView.setAdapter(adapter);
        listView.setOnScrollToTopListener(new MyRecyclerView.OnScrollToTopListener() {
            @Override
            public void refresh() {

                swipeLayout.setEnabled(true);
            }

            @Override
            public void notAtTopInScrollView(int offestY, int directionY) {
                swipeLayout.setEnabled(false);

            }
        });
        listView.setOnScrollToBottomListener(new MyRecyclerView.OnScrollToBottomListener() {
            @Override
            public void loadMore() {
                //Toast.makeText(DrawCircleActivity.this, "已到底部", Toast.LENGTH_SHORT).show();
                listViewLoadMore();
            }
        });
    }

    /**
     * 拉取数据请求
     */
    protected synchronized void listViewRefresh() {
        loadView.setVisibility(View.VISIBLE);
        nullContentLayout.setVisibility(View.GONE);
        last = "";
        if (adapter != null) {
            adapter.pullComplete(false);
        }
        DiaobaoSearchService.getInstance().searchHomeCartoon(currentKey ,1 , this ,activity , last);
    }

    /**
     * 加载更多
     */
    protected synchronized void listViewLoadMore() {
        if (hasMoreContent) {
            hasMoreContent = false;
            DiaobaoSearchService.getInstance().searchHomeCartoon(currentKey ,1 , this ,activity , last);
        }
    }

    @Override
    public void searchContent(String searchContent) {
        this.currentKey = searchContent;
        cleanData();
        listViewRefresh();
    }
    @Override
    public void cleanData() {
        if (adapter != null && mDataList != null) {
            mDataList.clear();
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void hideFilter(boolean isReset) {

    }

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        switch (handleType){
            case DiaobaoSearchService.HANDLE_TYPE_SEARCH_CARTOON:
                if(loadView .getVisibility() == View.VISIBLE){
                    loadView.setVisibility(View.GONE);
                }
                if(nullContentLayout != null){
                    nullContentLayout.setVisibility(View.GONE);
                }

                displayHttpData((org.json.JSONObject) response.getData());
                break;
        }
    }

    protected void displayHttpData(org.json.JSONObject data) {
        try {
            if (adapter == null) {
                return;
            }

            // 判断是否还有更多内容
            if ("null".equals(data.getString("last")) || !StringUtil.stringLength(data.getString("last")) || last.equals(data.getString("last"))) {
                hasMoreContent = false;
                adapter.pullComplete(true);

            } else {
                hasMoreContent = true;
                adapter.pullComplete(false);

            }
            // 如果是第一波数据
            if ("".equals(last) && mDataList != null) {
                mDataList.clear();
            }
            List dataList = com.alibaba.fastjson.JSONArray.parseArray(data.getJSONArray("list").toString(), CartoonBean.class);
            mDataList.addAll(dataList);

            adapter.notifyDataSetChanged();
            last = data.getString("last");

            if(mDataList.size() == 0){
                loadView.setVisibility(View.VISIBLE);
                nullContentLayout.setVisibility(View.VISIBLE);
            }
        } catch (JSONException e) {
            UIUtil.toastMessage(activity, "网络异常，请检查网络后再试");
            e.printStackTrace();
        }

    }
}
